package com.example.hand.service.impl;

import com.example.hand.bean.Order;
import com.example.hand.bean.PageBean;
import com.example.hand.bean.User;
import com.example.hand.dao.UserDao;
import com.example.hand.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public User getLogin(String user_name, String user_password) {
        return userDao.getLogin(user_name,user_password);
    }

    @Override
    public int saveUserInfo(User user) {
        return userDao.saveUserInfo(user);
    }

    @Override
    public List<User> findByUser(String user_name,String user_password,String user_age ,String user_sex) {
        return userDao.findByUser(user_name);
    }

    @Override
    public PageBean userPage(int currentPage) {
        HashMap<String,Integer> map = new HashMap<>();
        PageBean<User> pageBean = new PageBean();
        //封装当前页数
        pageBean.setCurrPage(currentPage);
        //封装每页显示的记录数
        int pageSize = 5;
        pageBean.setPageSize(pageSize);
        //封装总记录数
        int totalCount = userDao.selectCountUser();
        pageBean.setTotalCount(totalCount);
        //封装总页数
        double tc = totalCount;

        if(totalCount % pageSize ==0){
            pageBean.setTotalPage(totalCount/pageSize);
        }else {
            pageBean.setTotalPage(totalCount/pageSize+1);
        }
        if(currentPage == 0){
            System.out.println("已经是第一页了");

            map.put("start",currentPage);
            map.put("size",pageSize);

        }else {
            map.put("start",(currentPage-1)*pageSize);
            map.put("size",pageSize);
        }
        List<User> lists = userDao.userPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }


}
